﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;
using System.Lua;
using System.Lua.Serialization;
using System.Reflection;
using Sysyem.TextureSource;

namespace ConsoleApplication2
{
    public class OrderedItem
    {
        public string ItemName;
        public string Description;
        public decimal UnitPrice;
        public int Quantity;
        public decimal LineTotal;

        // A custom method used to calculate price per item.
        public void Calculate()
        {
            LineTotal = UnitPrice * Quantity;
        }

        public Vector2 Pos { set; get; }

        public List<OrderedItem> items;
        public OrderedItem item;

    }

    class Test
    {
        public static void Main(string[] args)
        {
            Test t = new Test();
            // Write a purchase order.
            t.SerializeObject("simple.xml");

            TestLuaSerializer();

            Console.Write("\r\nPress any key to continue....");
            Console.ReadKey();

        }

        private static void TestLuaSerializer()
        {
            object object1 = new Vector2();

            string luafile = "test.lua";
            StringBuilder stringBuilder;
            LuaWriter luaWriter;
            LuaSerializer luaSerializer;

            stringBuilder = new StringBuilder();
            luaWriter = LuaWriter.Create(stringBuilder);
            luaSerializer = new LuaSerializer(object1.GetType());

            luaSerializer.Serialize(luaWriter, object1);
            //Console.WriteLine(stringBuilder);
            StreamWriter file = new System.IO.StreamWriter(luafile);
            file.Write(stringBuilder);
            file.Close();

            object object2 = luaSerializer.Deserialize(luafile);

            writeProperty(object1);
            writeProperty(object2);
        }

        private static void writeProperty(object o)
        {
            Console.WriteLine(o.ToString());
            PropertyInfo[] pi = o.GetType().GetProperties();
            foreach (PropertyInfo p in pi)
            {
                Console.WriteLine(p.PropertyType + " " + p.Name + ":" + p.GetValue(o, null));
            }
        }

        private void SerializeObject(string filename)
        {
            Console.WriteLine("Writing With Stream");
            object testObject = CreateTestingObject();
            XmlSerializer serializer = new XmlSerializer(testObject.GetType());
           
            // Create a FileStream to write with.
            Stream writer = new FileStream(filename, FileMode.Create);
            // Serialize the object, and close the TextWriter
            serializer.Serialize(writer, testObject);
            writer.Close();
        }

        private void DeserializeObject(string filename)
        {
            
        }

        private object CreateTestingObject() {
            Texture t= new Texture();
            t.Add(new Textureregion());
            t.Add(new Textureregion());
            t.Add(new Textureregion());
            return t;
        }

    }
}
